<?php
class ext_detail_barang_keluar_model extends MY_Model {
	var $nama_tabel = 'ext_detail_barang';
	
	function GetJSON($idBM) {
		$ls_sql	= "	SELECT	a.id_detail_barang_pk id_detail_barang_pk,a.id_barang_fk  id_barang_fk, a.id_barang_keluar_fk id_barang_keluar_fk,  a.id_kondisi_fk id_kondisi_fk, a.jumlah_barang jumlah_barang, a.harga harga,a.total, b.nama_kondisi, c.nama_barang nama_barang
				FROM	".$this->nama_tabel." a
				left join ext_kondisi b 
				on a.id_kondisi_fk=b.id_kondisi_pk
				left join ext_barang c 
				on a.id_barang_fk = c.id_barang_pk
				WHERE 	id_barang_keluar_fk = '".$idBM."'
				ORDER BY id_barang_keluar_fk";

				$list = $this->db->query($ls_sql)->result_array();

		// Ambil Jumlah
		$this->db->select_sum('total');
		$this->db->select_sum('harga');
		$this->db->select_sum('jumlah_barang');
		$this->db->where('id_barang_keluar_fk', $idBM);
		$sum = $this->db->get('ext_detail_barang')->row_array();
		$footer[] = array('nama_kondisi' => '<b>Grand Total</b>','harga' => $sum['harga'], 'total' => $sum['total'],'jumlah_barang' => $sum['jumlah_barang']);
		$data = array('total' => count($list), 'rows' => $list, 'footer' => $footer);

		return json_encode($data);
	}

	function GenerateId() {
		$sql = "select nvl(max(id_detail_barang_pk), 0)+1 max 
				from ext_detail_barang
				";
		$data = $this->db->query($sql)->row_array();

		return intval($data['max']);
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_detail_barang_pk' => $arr_rule['id_detail_barang_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

}
?>